;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.PurchasePlanRpt", [])
        .controller("PurchasePlanRptCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "UserContextService", "$timeout", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, UserContextService, $timeout) {
            var btnPermissionCode = {
                purchasePlanAdd: window.ActionCode.purchasePlanAdd,
                purchasePlanEdit: window.ActionCode.purchasePlanEdit,
                purchasePlanDel: window.ActionCode.purchasePlanDel,
                purchasePlanAudit: window.ActionCode.purchasePlanAudit,
                purchasePlanManualFinish: window.ActionCode.purchasePlanManualFinish
            };
            var tabFlag = {
                0: "采购计划详情",
                1: "新增采购计划",
                2: "编辑采购计划"
            };

            $scope.activeFlag = [true, false, false, false, false, false];
            $scope.datePickerFlag = [false, false];
            $scope.tabFlag = {};
            $scope.btnPermission = {
                purchasePlanAdd: true,
                purchasePlanEdit: true,
                purchasePlanDel: true,
                purchasePlanAudit: true,
                purchasePlanManualFinish: true
            };
            $scope.markInfo = {
                allCheckedBill: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/purchasePlanRpt/export"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/purchasePlanRpt/exportAll")
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                AdvanceSearchAdd: false,
                AdvanceSearchLackAdd: false,
            };
            $scope.tableConfig = {
                fixedWidth: 80,
                scrollWidth: 1250,
                tableCol: [
                    {
                        type: "",
                        name: "序号",
                        dataField: "serialNumber",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "车型",
                        dataField: "vehicleType",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "分类",
                        dataField: "classify",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsName",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "机型",
                        dataField: "model",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "新加说明",
                        dataField: "descNew",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "供货商",
                        dataField: "vendorName",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    //	
                    {
                        type: "",
                        name: "P1",
                        dataField: "p1",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P2",
                        dataField: "p2",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P3",
                        dataField: "p3",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P4",
                        dataField: "p4",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P5",
                        dataField: "p5",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P6",
                        dataField: "p6",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P7",
                        dataField: "p7",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P8",
                        dataField: "p8",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P9",
                        dataField: "p9",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P10",
                        dataField: "p10",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P11",
                        dataField: "p11",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P12",
                        dataField: "p12",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "P13",
                        dataField: "p13",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },

                    {
                        type: "",
                        name: "12个月均需求量",
                        dataField: "PAvg",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "近13个月需求频率",
                        dataField: "PFrequency",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "预测的需求量D",
                        dataField: "forecast",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "需求量预测的标准差σ",
                        dataField: "standardDeviation",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    //							

                    {
                        type: "",
                        name: "近12个月平均需求量的方差δ",
                        dataField: "p12AvgVariance",
                        width: 220,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "当前月与平均需求量间的方差",
                        dataField: "pnAvgVariance",
                        width: 220,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "近13个月需求量与平均需求量之间的方差δ1",
                        dataField: "p13AvgVariance",
                        width: 250,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "近13个月需求订单总行数",
                        dataField: "saleOrderLineNumber",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "近13个月需求订单总数量",
                        dataField: "amountSale13",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "3个月出货量",
                        dataField: "amountOut3",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "6个月出货量",
                        dataField: "amountOut6",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    //					
                    {
                        type: "",
                        name: "12个月出货量",
                        dataField: "amountOut12",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "24个月出货量",
                        dataField: "amountOut24",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "本月缺项",
                        dataField: "amountLack",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "总部库存",
                        dataField: "amountStockHead",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "中心库",
                        dataField: "amountStockCentre",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    //					
                    {
                        type: "",
                        name: "总库存",
                        dataField: "amountStockTotal",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "在途",
                        dataField: "onwayAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "货期",
                        dataField: "deliveryDay",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "订货次数",
                        dataField: "orderAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "安全等级",
                        dataField: "securityLevel",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    //								
                    {
                        type: "",
                        name: "安全系数Z",
                        dataField: "securityLevelZ",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "安全库存SS",
                        dataField: "securityStock",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "周转储备库存LTD",
                        dataField: "cycleStockLtd",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "周转库存/经济订货量EOQ",
                        dataField: "cycleStockEoq",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "补货点ROP",
                        dataField: "cycleStockRop",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存上限",
                        dataField: "stockTop",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存上限（定）",
                        dataField: "",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划数量",
                        dataField: "planAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划数量（定）",
                        dataField: "",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "备注",
                        dataField: "memo",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                ]
            };
            $scope.auditStatusList = [{
                code: 0,
                name: "待审核"
            },
            {
                code: 1,
                name: "审核通过"
            },
            {
                code: 2,
                name: "审核拒绝"
            }
            ];
            $scope.sendStatusList = [{
                code: 0,
                name: "未创建"
            },
            {
                code: 1,
                name: "部分创建"
            },
            {
                code: 2,
                name: "已创建"
            }
            ];

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.buyPlan.submitted = false;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        $scope.tabFlag.type = 1;
                        $scope.tabFlag.name = tabFlag[1];
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        break;
                    case 6:
                        break;
                    default:
                        break;
                }
            };

            $scope.addPlan = function (type) {
                $scope.buyPlan.formInfo.headerInfo.stockOutFlag = type
                $scope.buyPlan.formInfo.materialList = [];
                setCurUserInfo4buyPlan();
                if (type == 0) {
                    $scope.materiels.setFixedWidth();
                    getMaterielList(1);
                    $scope.changeTab(1);
                } else if (type == 2) {
                    $scope.changeTab(6);
                    $scope.setFixedWidthLack();
                    getMaterielAllListLack(1);
                }
                else {
                    $scope.setFixedWidthLack();
                    getMaterielListLack(1);
                    $scope.changeTab(5);
                }
            };

            function setCurUserInfo4buyPlan() {
                $scope.buyPlan.formInfo.headerInfo.createName = $scope.curUser.name;
                $scope.buyPlan.formInfo.headerInfo.deptId = $scope.curUser.deptId;
                $scope.buyPlan.formInfo.headerInfo.deptName = $scope.curUser.deptName;
            }

            $scope.editPlan = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.buyPlan.formInfo = data;
                    $scope.tabFlag.type = 2;
                    $scope.tabFlag.name = tabFlag[2];
                    $scope.changeTab(2);
                });
            };

            $scope.auditPlan = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.buyPlan.formInfo = data;
                    $scope.changeTab(3);
                });
            };

            $scope.delPlan = function (item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/buyPlan/remove";
                    ybHttp.Post(service, reqUrl, [item.id]).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
            };

            $scope.manualFinish = function (id, code) {
                ybWidget.ShowMessage("确定要结单吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/buyPlan/manualFinish";
                    var param = {
                        id: id,
                        materialsNo: code
                    };
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        if (!code) {
                            getDataList();
                        } else {
                            viewDetail({
                                id: id
                            })
                        }
                    });
                })
            };

            $scope.nextStep = function () {
                $scope.buyPlan.formInfo.headerInfo.createDate = +new Date();
                if ($scope.activeFlag[6]) {
                    $scope.tabFlag.type = 1;
                    $scope.tabFlag.name = tabFlag[1]
                }
                if ($scope.buyPlan.formInfo.headerInfo.stockOutFlag == 1) {
                    $scope.tabFlag.type = 1;
                    $scope.tabFlag.name = tabFlag[1];

                    // 合并相同零件信息(qty取同一零件缺货数量之和)
                    const groups = {};
                    $scope.buyPlan.formInfo.materialList.forEach(g => {
                        const groupId = 'g' + g.materialsNo
                        groups[groupId] = groups[groupId] || []
                        groups[groupId].push(g)
                    })

                    const mArrTemp = []
                    Object.keys(groups).forEach(k => {
                        groups[k].sort((a, b) => b.deliveryDate - a.deliveryDate)
                        const deliveryDateNewestItem = groups[k][0]
                        deliveryDateNewestItem.qty = groups[k].reduce((total, cur) => total + cur.qty, 0)
                        deliveryDateNewestItem.qtyOrigin = deliveryDateNewestItem.qty
                        deliveryDateNewestItem.stockOutNo = groups[k].map(gk => gk.sourceOrder).join(',')
                        mArrTemp.push(deliveryDateNewestItem)
                    })
                    $scope.buyPlan.formInfo.materialList = mArrTemp
                }
                $scope.changeTab(2);
            };

            $scope.prevStep = function () {
                if ($scope.buyPlan.formInfo.headerInfo.stockOutFlag == 1) {
                    $scope.buyPlan.formInfo.materialList = [];
                    getMaterielListLack(1);
                    $scope.changeTab(5);
                } else {
                    getMaterielList(1);
                    $scope.changeTab(1);
                }
            };

            $scope.saveAuditPlan = function (type, id, message) {
                var service = "device",
                    reqUrl = "/buyPlan/audit",
                    param = {
                        id: id,
                        auditNote: message
                    };
                if (type === "refuse") {
                    if (!message) {
                        ybWidget.ShowMessage('请输入审核意见');
                        $scope.submitted = true;
                        return;
                    }
                    param.auditStatus = 2;
                } else {
                    param.auditStatus = 1;
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    $scope.buyPlan.formInfo.headerInfo.auditNote = "";
                });
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    }
                    /*,
                                         purchaseFixedWidth: {left: $scope.purchaseApplyConfig.fixedWidth + "px"}*/
                };
            };

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.materiels = {
                datePickerFlag: [false, false],
                markInfo: {
                    allCheckedBill: false,
                    curRow: -1
                },
                pageInfo: {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                },
                condition: {},
                dataList: [],
                tableConfig: { // 新增
                    fixedWidth: 40,
                    scrollWidth: 1600,
                    tableCol: [
                        {
                            type: "",
                            name: "零件编号",
                            dataField: "materialsNo",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "零件中文名",
                            dataField: "materialsDes",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "单位",
                            dataField: "unit",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "采购组",
                            dataField: "purchaseGroup",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "出库频次",
                            dataField: "salesRate",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "单价",
                            dataField: "price",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "库存上限",
                            dataField: "maxSafeAmount",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "实时库存",
                            dataField: "nowAmount",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "在途数量",
                            dataField: "onWayAmount",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "需求数量（月度）",
                            dataField: "monthAmount",
                            width: 140,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "需求数量（紧急）",
                            dataField: "urgentAmount",
                            width: 140,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "计划数量",
                            dataField: "planAmount",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "可替换零件编号",
                            dataField: "replaceMaterialsNo",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "可替换零件中文名",
                            dataField: "replaceMaterialsDes",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        },
                        {
                            type: "",
                            name: "可替换零件库存",
                            dataField: "replaceInventory",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                            toSort: false,
                            sortType: ""
                        }
                    ]
                },
                setFixedWidth: function () {
                    $scope.materiels.tbStyle = {
                        billFixedWidth: {
                            left: $scope.materiels.tableConfig.fixedWidth + "px"
                        },
                    }
                },
                search: function (curpage) {
                    getMaterielList(curpage);
                },
                tdClick: function (item, fnType) {
                    switch (fnType) {
                        case "viewDetail":
                            viewDetail(item);
                            break;
                        case "viewSapError":
                            viewSapError(item);
                            break;
                        case "downloadAttach":
                            downloadAttach(item);
                            break;
                        default:
                            break;
                    }
                },
                toSort: function (config, index) {
                    ybWidget.ToSort(config, index);
                },
                reset: function () {
                    $scope.materiels.condition = {};
                    $scope.materiels.condition.monthRequire = false;
                    $scope.materiels.condition.urgentRequire = false;
                },
                switchDatePicker: function (index) {
                    $scope.materiels.datePickerFlag = [false, false];
                    if (angular.isDefined(index)) {
                        $scope.materiels.datePickerFlag[index] = true;
                    }
                },
                checkDate: function (obj, startFiled, endFiled) {
                    var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                    if (!!timeFormatObj.tStart) {
                        obj[startFiled] = timeFormatObj.tStart;
                    }
                    if (!!timeFormatObj.tEnd) {
                        obj[endFiled] = timeFormatObj.tEnd;
                    }
                },
                selectRow: function (index) {
                    $scope.materiels.markInfo.cueRow = index
                },
                selectAll: function (list, type) {
                    angular.forEach(list, function (item) {
                        item.isChecked = $scope.materiels.markInfo["allChecked" + type];
                        asyncToSelected(item);
                    });
                },
                selectSingle: function (item, list, type) {
                    if (!item.isChecked) {
                        $scope.materiels.markInfo["allChecked" + type] = false;
                    } else {
                        if (!checkAll(list)) {
                            $scope.materiels.markInfo["allChecked" + type] = false;
                        } else {
                            $scope.materiels.markInfo["allChecked" + type] = true;
                        }
                    }
                    asyncToSelected(item);
                },
            };
            //数据导入
            $scope.importPlan = function () {
                var param = {
                    name: "采购计划基础资料",
                    templateReqService: "device",
                    templateReqUrl: "/purchasePlanData/downloadTemplate",
                    importReqService: "device",
                    importReqUrl: "/purchasePlanData/import",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };
            //计算
            $scope.compute = function () {

                var service = "device";
                var reqUrl = "/purchasePlanData/compute";
                var param = {};
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    getDataList(1);
                });
            };


            function initData() {
                $scope.tabFlag = {};
                $scope.buyPlan.formInfo = {
                    headerInfo: {},
                    materialList: []
                };
                $scope.materiels.condition.monthRequire = false;
                $scope.materiels.condition.urgentRequire = false;
            }

            function viewDetail(item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.buyPlan.formInfo = data;
                    $scope.changeTab(4);
                });
            }
            //加载数据
            function getDataList(curPage) {
                $scope.markInfo = {
                    allCheckedBill: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/purchasePlanRpt/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }
            //设置按钮权限
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function getMaterielList(curPage) {
                $scope.materiels.markInfo = {
                    allCheckedBill: false,
                    curRow: -1
                };

                if (!!curPage) {
                    $scope.materiels.pageInfo.curPage = curPage;
                }
                const filtersT = angular.copy($scope.materiels.condition);
                filtersT.monthRequire = filtersT.monthRequire ? 1 : 0;
                filtersT.urgentRequire = filtersT.urgentRequire ? 1 : 0;

                var param = {
                    pageSize: $scope.materiels.pageInfo.pageSize,
                    pageNum: $scope.materiels.pageInfo.curPage,
                    filters: filtersT
                };
                ybHttp.Post("device", "/buyPlan/reportList", param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        checkSelectMaterial(item);
                    });
                    $scope.materiels.markInfo.allCheckedBill = checkAll(resp.value.list);
                    $scope.materiels.pageInfo.totalRows = resp.value.total;
                    $scope.materiels.pageInfo.totalPages = resp.value.pages;
                    $scope.materiels.dataList = resp.value.list;
                });
            }
            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function checkAll(list) {
                var selectAllFlag = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAllFlag = false;
                        break;
                    }
                    selectAllFlag = true;
                }
                return selectAllFlag;
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);